package com.example.lib.utils.image_selector

import android.content.Context
import com.luck.picture.lib.basic.PictureSelector
import com.luck.picture.lib.config.SelectMimeType
import com.luck.picture.lib.config.SelectModeConfig
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.interfaces.OnResultCallbackListener

object ImageSelector {

    /**
     * 直接选择照片
     */
    fun choosePhoto(context: Context,
        max: Int = 1,
        min: Int = 1,
        single: Boolean = true,
        needCamera: Boolean = false,
        success: (result: ArrayList<LocalMedia?>) -> Unit) {
        PictureSelector.create(context).openGallery(SelectMimeType.ofImage())
            .setImageEngine(GlideEngine.createGlideEngine()).setMaxSelectNum(max).setMinSelectNum(min)
            .isDisplayCamera(needCamera)
            .setSelectionMode(if (single) SelectModeConfig.SINGLE else SelectModeConfig.MULTIPLE)
            .forResult(object : OnResultCallbackListener<LocalMedia?> {
                override fun onResult(result: ArrayList<LocalMedia?>) {
                    // 结果回调
                    success(result)
                }

                override fun onCancel() {
                    // 取消
                }
            })
    }

    /**
     * 去拍照
     */
    fun takePhoto(context: Context, success: (result: MutableList<LocalMedia>) -> Unit) {
        PictureSelector.create(context)
            .openCamera(SelectMimeType.ofImage())
            .forResult(object : OnResultCallbackListener<LocalMedia?> {
                override fun onResult(result: ArrayList<LocalMedia?>) {
                    // 结果回调
                    result.filterNotNull().toMutableList().let {
                        if (it.isNotEmpty()) success.invoke(it)
                    }
                }

                override fun onCancel() {
                    // 取消
                }
            })
    }
}